package aiowang.javaprimary.workqueues;

import aiowang.javaprimary.utils.RabbitMqUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Copyright (C), 2017-2021, aiowang
 * Author: aiowang
 * Date: 2021/8/25 16:33
 * FileName: Consumer
 * Description:
 */
public class Consumer2 {

    public static final String QUEUE_NAME= "myworkqueue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitMqUtils.getConnection();
        Channel channel = connection.createChannel();
       // channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //同一时刻服务器只发送一条消息给消费端
        channel.basicQos(1);
        //推送的消息如何进行消费的接口回调
        DeliverCallback deliverCallback=(consumerTag, delivery)->{
            String message= new String(delivery.getBody());
            System.out.println("Consumer2---->" + message);
        };

        //取消消费的一个回调接口 如在消费的时候队列被删除掉了
        CancelCallback cancelCallback=(consumerTag)->{
            System.out.println("Consumer2 消息消费被中断");
        };

        /**
         *  消费者消费消息
         * 1. 消费哪个队列
         * 2. 消费成功之后是否要自动应答 true 代表自动应答 false 手动应答
         * 3. 消费者未成功消费的回调
         */
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
    }

}
